package Time_Clock;

import javax.swing.*;
import java.applet.*;
import java.awt.*;
import java.net.*;


public class Alarm extends Applet implements  Runnable{
    private Time time = new Time();
    private Time finaltime = new Time();
    private FlagAnthemPanel flagAnthemPanel = new FlagAnthemPanel();
    private boolean set = false;

    @Override
    public void init(){
        super.init();
        add(flagAnthemPanel);
    }

    @Override
    public void start() {
        Thread clockThread = new Thread(this, "Showtime");
        clockThread.start();
    }

    @Override
    public void paint(Graphics g){
        super.paint(g);
        String s = "elapsed_time:";
        g.drawString(s,75, 20);
        g.drawRect(0,0,getWidth()-1,getHeight()-1);
        flagAnthemPanel.paintComponent(g);
        paint_clock(g);
    }

    @Override
    public void run() {
        while (true)
        {
            repaint();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if(set && time.getMinute() == finaltime.getMinute() && time.getHour() == finaltime.getHour()) {
                try {
                    AudioClip audioClip = newAudioClip(new URL("file:///C:/Users/59565/eclipse-workspace/Curriculum Design/東方月まで届け、不死の烟.mid"));
                    audioClip.play();
                    set = false;
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                }
            }

        }
    }


    private void paint_clock(Graphics g){
        int centerX = getSize().width / 2;
        int centerY = getSize().height / 2 + 10;
        int radius = (int) (Math.min(getSize().width, getSize().height) * 0.8 * 0.5);
        g.setColor(Color.BLACK);
        g.drawLine(0, 350, 600, 350);
        g.drawOval(centerX - radius, centerY - radius, radius * 2, radius * 2);
        g.setFont(new Font("Tahoma", Font.PLAIN, 20));
        g.drawString("3", centerX + radius - 10, centerY + 10);
        g.drawString(" 6", centerX - 10, centerY + radius);
        g.drawString("9", centerX - radius, centerY + 10);
        g.drawString("12", centerX - 10, centerY - radius + 20);

        int sLength = (int) (radius * 0.8);
        int secondX = (int) (centerX + sLength * Math.sin(time.getSecond() * (2 * Math.PI / 60)));
        int secondY = (int) (centerY - sLength * Math.cos(time.getSecond() * (2 * Math.PI / 60)));
        g.setColor(Color.RED);
        g.drawLine(centerX, centerY, secondX, secondY);

        int mLength = (int) (radius * 0.65);
        int minuteX = (int) (centerX + mLength * Math.sin(time.getMinute() * (2 * Math.PI / 60)));
        int minuteY = (int) (centerY - mLength * Math.cos(time.getMinute() * (2 * Math.PI / 60)));
        g.setColor(Color.BLUE);
        g.drawLine(centerX, centerY, minuteX, minuteY);

        int hLength = (int) (radius * 0.5);
        int hourX = (int) (centerX + hLength * Math.sin((time.getHour() % 12 + time.getMinute() / 60.0) * (2 * Math.PI / 12)));
        int hourY = (int) (centerY - hLength * Math.cos((time.getHour() % 12 + time.getMinute() / 60.0) * (2 * Math.PI / 12)));
        g.setColor(Color.GREEN);
        g.drawLine(centerX, centerY, hourX, hourY);
    }

    public class FlagAnthemPanel extends JPanel {
        FlagAnthemPanel( ) {
            setLayout(new FlowLayout());
            TextField textField = new TextField(2);
            add(textField);
            textField.addActionListener(arg0 -> {
                int elapsed_time;
                finaltime.setCurrentTime();
                if (!textField.getText().equals("")) {
                    elapsed_time = Integer.parseInt(textField.getText());
                    finaltime.setMinute(time.getMinute() + elapsed_time);
                    while (finaltime.getMinute() >= 60) {
                        finaltime.setMinute(finaltime.getMinute() - 60);
                        finaltime.setHour((finaltime.getHour() + 1) % 24);
                    }
                    set = true;
                }
            });
        }
        public void paintComponent(Graphics g) {
            Integer resetminute = 0;
            time.setCurrentTime();
            if (set) {
                if (time.getHour() > finaltime.getHour() ||
                        (time.getHour() == finaltime.getHour() && time.getMinute() > finaltime.getMinute()))
                    finaltime.setHour(finaltime.getHour() + 24);
                resetminute = (60 - time.getMinute() + finaltime.getMinute()) + (finaltime.getHour() - time.getHour() - 1) * 60;
            }

            g.drawString("Rest Time:",230, 20);
            g.drawString(resetminute.toString(),300,20);
            g.drawString("minutes", 340, 20);
        }
    }
}
